﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace deFramework.Common
{
    /// <summary>
    /// .
    /// </summary>
    public class PresentationContext
    {

        #region "Constructors"

        /// <summary>
        /// 
        /// </summary>
        public PresentationContext()
        {
            // do nothing 
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aWeb"></param>
        /// <param name="aPage"></param>
        public PresentationContext(deFramework.Base.BaseWebService aService)
        {
            try
            {
                this.WebService = aService; 
                this.MyLog = new FrameworkLogging(this.AssemblyName, this.ProjectName);
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aWeb"></param>
        /// <param name="aPage"></param>
        public PresentationContext(ref Microsoft.SharePoint.SPWeb aWeb, System.Web.UI.Page aPage)
        {
            try
            {
                this.Page = aPage;
                this.MyLog = new FrameworkLogging(this.AssemblyName, this.ProjectName);
                this.MySpWeb = aWeb;
                this.SpWebObjectInContext = true;
                this.WebTitle = aWeb.Title;
            }
            catch (Exception ex)
            {
                this.CurrentException = ex; 
            }

        }

        /// <summary>
        /// 
        /// </summary>
        public string WebTitle = "unspecified"; 
        //Microsoft.SharePoint.

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aWeb"></param>
        /// <param name="aWebPart"></param>
        public PresentationContext(ref Microsoft.SharePoint.SPWeb aWeb,
            Base.BaseWebPart aWebPart)
        {
            this.MyLog = new FrameworkLogging(this.AssemblyName, this.ProjectName);
            this.MySpWeb = aWeb;
            this.Page = aWebPart.Page;
            this.SpWebObjectInContext = true;
            this.ActivePart = aWebPart;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aProperties"></param>
        public PresentationContext(Microsoft.SharePoint.SPFeatureReceiverProperties aProperties)
        {
            this.MyLog = new FrameworkLogging(this.AssemblyName, this.ProjectName);
            this.MySpWeb = aProperties.Feature.Parent as Microsoft.SharePoint.SPWeb; //this.FeatureSiteObject.RootWeb;
            this.FeatureInContext = true;
            this.FeatureProperties = aProperties;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aProperties"></param>
        public PresentationContext(Microsoft.SharePoint.SPItemEventProperties aProperties)
        {
            this.MyLog = new FrameworkLogging(this.AssemblyName, this.ProjectName);
            this.MySpWeb = aProperties.OpenWeb() as Microsoft.SharePoint.SPWeb; //this.FeatureSiteObject.RootWeb;
            this.ItemEventInContext = true;
            this.ItemEventProperties = aProperties;
        }


        #endregion
            
        #region "Containers - Page, ActivePart, ActiveUserControl"

        /// <summary>
        /// 
        /// </summary>
        public System.Web.UI.Page Page = null;

        /// <summary>
        /// 
        /// </summary>
        public deFramework.Base.BaseWebService WebService = null; 

        /// <summary>
        /// 
        /// </summary>
        public Base.BaseWebPart ActivePart = null;

        /// <summary>
        /// 
        /// </summary>
        public Base.BaseUserControl ActiveUserControl = null;


        #endregion

        #region "Polymorphism - PageParamValue, SpWebPropertyValue, SetSpWebProperty, RemoveSpWebProperty"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aVariableName"></param>
        /// <returns></returns>
        public bool SearchCalled(string aVariableName)
        {

            bool rtn_value = false;

            if (Page.Request.Form[aVariableName] == null)
            {
                if (Page.Request.QueryString[aVariableName] != null)
                {
                    rtn_value = true;
                }
            }
            else
            {
                rtn_value = true;
            }

            return rtn_value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aVariableName"></param>
        /// <param name="aDefault"></param>
        /// <returns></returns>
        public string PageParamValue(string aVariableName, string aDefault)
        {
            string l_FormValue = this.PageFormStringValue(aVariableName, aDefault);
            string l_QueryValue = this.PageQueryStringValue(aVariableName, aDefault);
            if (l_FormValue != aDefault)
                return l_FormValue;
            else
                return l_QueryValue;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aPropertyName"></param>
        /// <param name="aDefault"></param>
        /// <returns></returns>
        public string SpWebPropertyValue(string aPropertyName, string aDefault)
        {
            string l_Result = aDefault;
            try
            {
                l_Result = this.MySpWeb.AllProperties[aPropertyName].ToString();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName"></param>
        /// <returns></returns>
        public bool RemoveSpWebProperty(string aName)
        {
            bool l_Result = false;
            try
            {
                this.MySpWeb.Properties.Remove(aName);
                l_Result = true;
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName"></param>
        /// <param name="aValue"></param>
        /// <returns></returns>
        public bool SetSpWebProperty(string aName, string aValue)
        {
            bool l_Result = false;
            try
            {
                this.MySpWeb.Properties.Add(aName, aValue);
                l_Result = true;
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Result;
        }



        #endregion

        #region "Polymorphism - MyLog, CurrentException, StandardLog, GotoErrorPage"

        /// <summary>
        /// 
        /// </summary>
        public FrameworkLogging MyLog = null;

        /// <summary>
        /// 
        /// </summary>
        public System.Exception CurrentException
        {
            get { return this._CurrentException; }
            set { this._CurrentException = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aMethodName"></param>
        /// <param name="aMessage"></param>
        /// <returns></returns>
        public bool StandardLog(string aMethodName, string aMessage)
        {
            return this.MyLog.StandardLog(aMethodName, "information", aMessage);
        }

        /// <summary>
        /// STUB for now - todo to send this to an error page. 
        /// </summary>
        /// <param name="aTitle"></param>
        /// <param name="aDescription"></param>
        public void GotoErrorPage(string aTitle, string aDescription)
        {
            string l_Url = String.Format(this.ErrorPageFormatString,
                this.Page.Server.HtmlEncode(aTitle),
                this.Page.Server.HtmlEncode(aDescription));
            this.StandardLog(aTitle, "GotoErrorPage: " + aDescription);
            this.Page.Response.Redirect(l_Url);    
        }

        #endregion

        #region "Polymorphism - RunModeConfigSetting, ConfigAppSettingValue, Sha1EncryptValue"

        public string RunModeConfigSetting(string key, string defaultValue)
        {
            string _result = defaultValue;
            try
            {
                string runMode = this.ConfigAppSettingValue("RunMode", "Dev");
                _result = System.Configuration.ConfigurationManager.AppSettings[runMode + key].ToString();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return _result;
        }

        public string ConfigAppSettingValue(string fullSettingName, string defaultValue)
        {

            string _result = defaultValue;
            try
            {
                _result = System.Configuration.ConfigurationManager.AppSettings[fullSettingName].ToString();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return _result;

        }

        public string Sha1Encryption(string sourceConfigSettingName)
        {
            string _result = String.Empty;
            try
            {
                string sourceValue = this.RunModeConfigSetting(sourceConfigSettingName, String.Empty);
                string sharedKey = this.RunModeConfigSetting("Sha1SharedKey", String.Empty);
                deFramework.Common.EncryptionTools encrypter = new deFramework.Common.EncryptionTools();
                _result = encrypter.HMACSHA1Output(sourceValue, sharedKey);
            }
            catch (Exception ex)
            {
                this.CurrentException = ex; 
            }
            return _result;
        }


        #endregion 


        #region "public - CurrentDataIntegerValue, CurrentDataRecordType, ErrorPageFormatString"

        /// <summary>
        /// 
        /// </summary>
            public int CurrentDataIntegerValue = -1;

            /// <summary>
            /// 
            /// </summary>
            public string CurrentDataRecordType = "none";

            /// <summary>
            /// 
            /// </summary>
            public string ErrorPageFormatString = "Error.aspx?title={0}&description={1}";

        #endregion

        #region "public - SiteUrl, AssemblyName, ProjectName, MySpWeb"

            /// <summary>
            /// 
            /// </summary>
            public string SiteUrl = String.Empty;
            /// <summary>
            /// 
            /// </summary>
            public string AssemblyName = "unspecified";
            /// <summary>
            /// 
            /// </summary>
            public string ProjectName = "unspecified";
            /// <summary>
            /// 
            /// </summary>
            public Microsoft.SharePoint.SPWeb MySpWeb = null;


        #endregion

        #region "public methods - LoggingCodeName, Dispose"


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aAssemblyName"></param>
        /// <param name="aProjectName"></param>
        public void LoggingCodeName(string aAssemblyName, string aProjectName)
        {
            this.MyLog.SetCodeName(aAssemblyName, aProjectName);
        }


        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            try
            {
                this.MySpWeb.Dispose();
            }
            catch (Exception ex)
            {
                this.MyLog.StandardLog("Dispose", "information", ex.Message);

            }
        }


        #endregion

        #region "Public Properties - UseControlOverridesForProcessing, LinkToPageUrl, LinkToParamName, IntakeParamName, ExitToPageUrl"



        /// <summary>
        /// The BaseUserControl class contains 4 properties available for web parts to override 
        /// intake parameter names, outgoing URL for success/cancel and outgoing parameter names. 
        /// Should the business rule objects use these overrides rather than the hard-coded constants?
        /// For example, you have a business rule object that expects a parameter to come in as a Form 
        /// variable or querystring variable named ANSWER but the web part user has a page linking into 
        /// this page where the parameter is named QUESTION. The web part user enters in the IntakeParamName 
        /// property value to be QUESTION. Thus if this UseControlOverridesForProcessing value is set as TRUE 
        /// then the override will be allowed. If it isn't then the business rule object will expect the default 
        /// parameter name of ANSWER. This UseControlOverridesForProcessing value would be set in the codebehind 
        /// of the user control BEFORE the Facade call is made to create/use the business rule object. 
        /// </summary>
        public bool UseControlOverridesForProcessing
        {

            get { return this._UseControlOverridesForProcessing; }
            set
            {
                if (!value || this.ActiveUserControl == null)
                {
                    this._UseControlOverridesForProcessing = false;
                    this._LinkToPageUrl = String.Empty;
                    this._LinkToParamName = String.Empty;
                    this._IntakeParamName = String.Empty;
                    this._ExitToPageUrl = String.Empty;
                }
                else
                {
                    this._UseControlOverridesForProcessing = true;
                    this._LinkToPageUrl = this.ActiveUserControl.LinkToPageUrl;
                    this._LinkToParamName = this.ActiveUserControl.LinkToParamName;
                    this._IntakeParamName = this.ActiveUserControl.IntakeParamName;
                    this._ExitToPageUrl = this.ActiveUserControl.ExitToPageUrl;
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        public string LinkToPageUrl
        {
            get { return this._LinkToPageUrl; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string LinkToParamName
        {
            get { return this._LinkToParamName; }

        }

        /// <summary>
        /// 
        /// </summary>
        public string IntakeParamName
        {
            get { return this._IntakeParamName; }

        }

        /// <summary>
        /// 
        /// </summary>
        public string ExitToPageUrl
        {
            get { return this._ExitToPageUrl; }

        }

        #endregion
 
        #region "Public Properties - SpWebInContext, FeatureProperties, ItemEventProperties, FeatureInContextb"

        /// <summary>
        /// 
        /// </summary>
        public bool SpWebInContext
        {
            get { return (this.FeatureInContext || this.ItemEventInContext || this.SpWebObjectInContext); }
        }

        /// <summary>
        /// 
        /// </summary>
        public Microsoft.SharePoint.SPFeatureReceiverProperties FeatureProperties = null;
        
        /// <summary>
        /// 
        /// </summary>
        public Microsoft.SharePoint.SPItemEventProperties ItemEventProperties = null;

        /// <summary>
        /// 
        /// </summary>
        public bool FeatureInContext = false;


        #endregion

        #region "Private - Variables, Constants"


        private bool ItemEventInContext = false;
        private bool SpWebObjectInContext = false;
        private Exception _CurrentException = null;
        private bool _UseControlOverridesForProcessing = false;

        private string _LinkToPageUrl = String.Empty;
        private string _LinkToParamName = String.Empty;
        private string _IntakeParamName = String.Empty;
        private string _ExitToPageUrl = String.Empty;



        #endregion
        
        #region "private subroutines - PageQueryStringValue, PageFormStringValue"

        private string PageQueryStringValue(string aVariableName, string aDefault)
        {
            string l_Result = aDefault;
            try
            {
                l_Result = this.Page.Request.QueryString[aVariableName].ToString();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Result;
        }

        private string PageFormStringValue(string aVariableName, string aDefault)
        {
            string l_Result = aDefault;
            try
            {
                l_Result = this.Page.Request.Form[aVariableName].ToString();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Result;
        }


        #endregion 

        /// <summary>
        /// 
        /// </summary>
        public System.Collections.Specialized.StringDictionary NameValueList = new System.Collections.Specialized.StringDictionary();

        
    }
}

#region "Dead Code" 

        //public string SiteUrl  = String.Empty; 
        //public string AssemblyName = "unspecified"; 
        //public string ProjectName = "unspecified"; 
        //public MySharepointLogging mLog = null; 


       //this.mLog = new IceSharepointLogging(this.AssemblyName, this.ProjectName); 



//    #region "Constructors"

//    public PresentationContext()
//    {
//        // do nothing 
//    }


//    public PresentationContext(ref SPWeb aWeb, System.Web.UI.Page aPage)
//    {
//        this.MySpWeb = aWeb;
//        this.Page = aPage;
//        this.SpWebObjectInContext = true;
//        try
//        {
//            this.IcePage = (Dhs.Ice.Sharepoint.Ofm.Base.BaseIcePage)aPage;
//        }
//        catch (Exception ex)
//        {
//            this.CurrentException = ex;
//        }
//    }

//    public PresentationContext(ref SPWeb aWeb, Dhs.Ice.Sharepoint.Ofm.Base.BaseIcePage aPage)
//    {
//        this.mLog = new IceSharepointLogging(this.AssemblyName, this.ProjectName);
//        this.MySpWeb = aWeb;
//        this.Page = aPage;
//        this.SpWebObjectInContext = true;
//        this.IcePage = aPage;
//    }



//    public PresentationContext(ref SPWeb aWeb,
//        Dhs.Ice.Sharepoint.Ofm.Base.BaseUserControlContainerWebPart aWebPart)
//    {
//        this.mLog = new IceSharepointLogging(this.AssemblyName, this.ProjectName);
//        this.MySpWeb = aWeb;
//        this.Page = aWebPart.Page;
//        this.SpWebObjectInContext = true;
//        this.IcePart = aWebPart;
//    }



//    public PresentationContext(SPFeatureReceiverProperties aProperties)
//    {
//        this.mLog = new IceSharepointLogging(this.AssemblyName, this.ProjectName);
//        this.MySpWeb = aProperties.Feature.Parent as SPWeb; //this.FeatureSiteObject.RootWeb;
//        this.FeatureInContext = true;
//        this.FeatureProperties = aProperties;
//    }




//    public PresentationContext(SPItemEventProperties aProperties)
//    {
//        this.mLog = new IceSharepointLogging(this.AssemblyName, this.ProjectName);
//        this.MySpWeb = aProperties.OpenWeb() as SPWeb; //this.FeatureSiteObject.RootWeb;
//        this.ItemEventInContext = true;
//        this.ItemEventProperties = aProperties;
//    }



//        //public SPSite FeatureSiteObject = null; 


//#endregion 


//    #region "Private - Variables, Constants"


//    private Base.BaseUserControl _ActiveUserControl = null;
//    private bool ItemEventInContext = false;
//    private bool SpWebObjectInContext = false;
//    private Exception _CurrentException = null;
//    private bool _UseControlOverridesForProcessing = false;

//    private string _LinkToPageUrl = String.Empty;
//    private string _LinkToParamName = String.Empty;
//    private string _IntakeParamName = String.Empty;
//    private string _ExitToPageUrl = String.Empty;



//    //public SPSite FeatureSiteObject = null; 

//    #endregion 


//#region "public - SiteUrl, AssemblyName, ProjectName, mLog, MySpWeb" 

//    public Microsoft.SharePoint.SPWeb MySpWeb = null; 


//#endregion 	

//#region "public methods - LogCodeName, FeatureIntegrate, ItemEventIntegrate, Dispose" 



//    public void LogCodeName(string aAssemblyName, string aProjectName) 
//    {
//       //this.mLog.SetCodeName(aAssemblyName, aProjectName); 
//    }


//    public void FeatureIntegrate(Microsoft.SharePoint.SPFeatureReceiverProperties aProperties) 
//    {
//       this.FeatureInContext = true;
//       this.FeatureProperties = aProperties;
//       this.MySpWeb = aProperties.Feature.Parent as Microsoft.SharePoint.SPWeb; //this.FeatureSiteObject.RootWeb;
//    }

//    public void ItemEventIntegrate(Microsoft.SharePoint.SPItemEventProperties aProperties) 
//    {
//       this.ItemEventInContext = true;
//       this.ItemEventProperties = aProperties;
//       this.MySpWeb = aProperties.OpenWeb() as Microsoft.SharePoint.SPWeb; //this.FeatureSiteObject.RootWeb;
//    }

//    public void Dispose() 
//    {
//        try 
//        {
//            this.MySpWeb.Dispose(); 
//        }
//        catch 
//        {

//        }
//    }

//       //this.FeatureSiteObject = (SPSite)properties.Feature.Parent;

//       //this.FeatureSiteObject = (SPSite)properties.Feature.Parent;


//#endregion 

//#region "Properties - SpWebInContext, FeatureProperties, ItemEventProperties, FeatureInContext"

//    public bool SpWebInContext
//    {
//        get { return (this.FeatureInContext || this.ItemEventInContext); }
//    }

//    public Microsoft.SharePoint.SPFeatureReceiverProperties FeatureProperties = null;
//    public Microsoft.SharePoint.SPItemEventProperties ItemEventProperties = null;
//    public bool FeatureInContext = false;
//    public bool ItemEventInContext = false; 



//#endregion 
		
		


#endregion 